<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>模板管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			$("#file").change(function () {
		        var filepath = $("input[name='file']").val();
		        var extStart = filepath.lastIndexOf(".");
		        var ext = filepath.substring(extStart, filepath.length).toUpperCase();
		        if (ext != ".xml" && ext != ".XML") {
		          alertx("文件限于xml格式");
		          $("#fileSize").text("");
		          $("#file").val("");
		          $("#hiddenFile").val("");
		          return false;
		        }
		        var file_size = 0;
		        if ($.browser.msie) {
		          var img = new Image();
		          img.src = filepath;
		          while (true) {
		            if (img.fileSize > 0) {
		              if (img.fileSize > 3 * 1024 * 1024) {
		                alertx("文件不大于100MB。");
		                $("#file").val("");
		              } else {
		                var num03 = img.fileSize / 1024;
		                num04 = num03.toFixed(2)
		                $("#fileSize").text(num04 + "KB");
		              }
		              break;
		            }
		          }
		        } else {
		          file_size = this.files[0].size;
		          var size = file_size / 1024;
		          if (size > 1024*20) {
		            alert("上传的文件大小不能超过20M！");
		            $("#file").val("");
		          } else {
		            var num01 = file_size / 1024;
		            num02 = num01.toFixed(2);
		            $("#fileSize").text(num02 + " KB");
		          }
		        }
		        return true;
		      });
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					if($("#hiddenFile").val()==""){
						alertx("请上传模板");
					}else{
						loading('正在提交，请稍等...');
						form.submit();
					}
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				},
				rules:{
					name:{
						remote: {
						    url: $("#ctx").val()+"/ins/insTemplate/getByName",     //后台处理程序
						    type: "post",               //数据发送方式
						    dataType: "json",           //接受数据格式   
						    data: {                     //要传递的数据
						        name: function() {
						            return $("#name").val();
						        },
						        id: function(){
						        	return $("#id").val();
						        }
						    }
						}
					} 
				},
				messages:{
					name:{
						remote:"该模板名称已存在！"
					} 
				}
			});
			
			$("#file").change(function(){
				$("#hiddenFile").val($(this).val());
			});
			$("#btnEdit").click(function(){
				$("#fileDiv").show();
				$("#buttonDiv").hide();
			});
			if($("#hiddenFile").val() != ""){
				$("#fileDiv").hide();
				$("#buttonDiv").show();
			}
		});
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/ins/insTemplate/">模板列表</a></li>
		<li class="active"><a href="${ctx}/ins/insTemplate/form?id=${insTemplate.id}">模板<shiro:hasPermission name="ins:insTemplate:edit">${not empty insTemplate.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="ins:insTemplate:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="insTemplate" enctype="multipart/form-data" action="${ctx}/ins/insTemplate/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<input type="hidden" id="ctx" value="${ctx}"/>
		<sys:message content="${message}"/>		
		<div class="control-group">
			<label class="control-label">模板名称：</label>
			<div class="controls">
				<form:input path="name" htmlEscape="false" maxlength="64" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">备注：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="1000" class="input-xxlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">文件：</label>
			<div id="fileDiv" class="controls">
				<input id="file" class="input-xlarge" type="file" value="${insTemplate.src}" name="file"/>
				<span class="help-inline"><font color="red">*</font> </span>
				<li id="fileSize"></li>
				<input id="hiddenFile" name="hiddenFile" type="hidden" value="${insTemplate.src}" class="required"/>
			</div>
			<div id="buttonDiv" class="controls" style="display:none;">
				<font>${insTemplate.originName}</font>
				<input id="btnEdit" class="btn btn-primary" type="button" value="修改"/>
			</div>
		</div>
		<div class="form-actions">
			<shiro:hasPermission name="ins:insTemplate:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>